<?php


/* 获得商品列表 */
function get_sql_exchange_goodslist(){
	
	$CommonModel				=	new CommonModel();
	
        $filter 					= 	array();
        $filter['keyword']    		= 	empty($_REQUEST['keyword']) ? '' : trim($_REQUEST['keyword']);
        if (isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax'] == 1){
            $filter['keyword'] 		= 	$filter['keyword'];
        }
        $filter['sort_by']    		= 	empty($_REQUEST['sort_by']) ? 'eg.goods_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] 		= 	empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);

        $where 						=	'';
        if (!empty($filter['keyword'])){
            $where 					= 	" AND g.goods_name LIKE '%" . Utils::mysql_like_quote($filter['keyword']) . "%'";
        }

        /* 获取文章数据 */
        $sql 						= 	'SELECT eg.* , g.goods_name '.
               							'FROM ' .'ybr_exchange_goods' . ' AS eg '.
               							'LEFT JOIN ' .'ybr_goods' . ' AS g ON g.goods_id = eg.goods_id '.
               							'WHERE 1 ' .$where. ' ORDER by '.$filter['sort_by'].' '.$filter['sort_order'];

   	return $sql;
}

/**
 * 取得商品列表：用于把商品添加到组合、关联类、赠品类
 * @param   object  $filters    过滤条件
 */
function get_goods_list($filter){
    $filter['keyword']	= 	$filter['keyword'];
    $where 				= 	get_where_sql($filter); // 取得过滤条件
    
    /* 取得数据 */
    $sql = 'SELECT goods_id, goods_name, shop_price '.
           'FROM ' . 'ybr_goods' . ' AS g ' . $where .
           'LIMIT 50';    
    //echo $sql;    
    $row 				= 	M()->query($sql);
    return $row;
}


/**
 * 生成过滤条件：用于 get_goodslist 和 get_goods_list
 * @param   object  $filter
 * @return  string
 */
function get_where_sql($filter){
    $time 		= 	date('Y-m-d');

    $where  	= 	isset($filter['is_delete']) && $filter['is_delete'] == '1' ?' WHERE is_delete = 1 ' : ' WHERE is_delete = 0 ';
    $where 		.= 	(isset($filter['real_goods']) && ($filter['real_goods'] > -1)) ? ' AND is_real = ' . intval($filter['real_goods']) : '';
    $where 		.= 	isset($filter['cat_id']) && $filter['cat_id'] > 0 ? ' AND ' . get_children($filter['cat_id']) : '';
    $where 		.= 	isset($filter['brand_id']) && $filter['brand_id'] > 0 ? " AND brand_id = '" . $filter['brand_id'] . "'" : '';
    $where 		.=	isset($filter['intro_type']) && $filter['intro_type'] != '0' ? ' AND ' . $filter['intro_type'] . " = '1'" : '';
    $where 		.= 	isset($filter['intro_type']) && $filter['intro_type'] == 'is_promote' ?
        			" AND promote_start_date <= '$time' AND promote_end_date >= '$time' " : '';
    $where 		.= 	isset($filter['keyword']) && trim($filter['keyword']) != '' ?
        			" AND (goods_name LIKE '%" . Utils::mysql_like_quote($filter['keyword']) . "%' OR goods_sn LIKE '%" . Utils::mysql_like_quote($filter['keyword']) . "%' OR goods_id LIKE '%" . Utils::mysql_like_quote($filter['keyword']) . "%') " : '';
    $where 		.= 	isset($filter['suppliers_id']) && trim($filter['suppliers_id']) != '' ?
        			" AND (suppliers_id = '" . $filter['suppliers_id'] . "') " : '';

    $where 		.= 	isset($filter['in_ids']) ? ' AND goods_id ' . db_create_in($filter['in_ids']) : '';
    $where 		.=	isset($filter['exclude']) ? ' AND goods_id NOT ' . db_create_in($filter['exclude']) : '';
    $where 		.= 	isset($filter['stock_warning']) ? ' AND goods_number <= warn_number' : '';
    return $where;
}

